<template>
  <div class="loginPage">
    <div class="closeBtn">
      <span class="iconfont iconicon-test" style="color: #fff"></span>
    </div>
    <div class="logoPart">
      <span class="iconfont iconnew"></span>
    </div>
    <AuthInput
      :msg="{ holder: '请输入电话号码', type: 'text' }"
      :rule="/^\d{5,11}$/"
      err_msg="请输入正确的号码"
      @valChange="setUsername"
    ></AuthInput>
    <AuthInput
      :msg="{ holder: '请输入密码', type: 'password' }"
      :rule="/^\w{3,8}$/"
      err_msg="密码不合法"
      @valChange="setPassword"
    ></AuthInput>
    <AuthBtn msg="登录" @submit="login"></AuthBtn>
    <div class="otherWay">---------------其他登录方式---------------</div>
    <div class="way">
      <div class="weixin"></div>
      <div class="qq"></div>
    </div>
  </div>
</template>

<script>
import AuthInput from "../components/AuthInput";
import AuthBtn from "../components/AuthBtn";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: { AuthBtn, AuthInput },
  methods: {
    setUsername(newUser) {
      this.username = newUser;
    },
    setPassword(newPass) {
      this.password = newPass;
    },
    login() {
      console.log("正在执行登录");
      console.log("用户名", this.username);
      console.log("密码", this.password);
      //发送前先校验输入是否正确
      if (!this.username || !this.password) {
        this.$toast.fail("请输入完整信息");
        return;
      }
      //发送ajax请求
      this.$axios({
        url: "/login",
        method: "post",
        data: {
          username: this.username,
          password: this.password,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.message == "登录成功") {
            this.$toast.success(res.data.message);
            //登录成功储存token
            console.log(res);
            localStorage.setItem("token", res.data.data.token);
            localStorage.setItem("userId", res.data.data.user.id);
            //跳转到个人中心页面
            setTimeout(() => {
              this.$router.push("/");
            }, 600);
          }
        })
        .catch((err) => {
          console.dir(err);
          this.$toast.fail("系统错误");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.loginPage {
  padding: 24/360 * 100vw;
  height: 100%;
  background: url("../assets/back.jpeg") no-repeat;
  background-size: 100% 100%;
  .iconicon-test {
    font-size: 27/360 * 100vw;
  }
  .logoPart {
    text-align: center;
    .iconnew {
      font-size: 126/360 * 100vw;
      color: red;
    }
  }
  .otherWay {
    color: #fff;
    font-size: 20px;
    text-align: center;
    margin: 30px 0;
  }
  .way {
    display: flex;
    justify-content: space-around;
    .weixin {
      width: 50/360 * 100vw;
      height: 50/360 * 100vw;
      background: url("../assets/WeChat.png") no-repeat;
      background-size: 100%;
      transform: translateX(30px);
    }
    .qq {
      width: 48/360 * 100vw;
      height: 48/360 * 100vw;
      background: url("../assets/QQ.png") no-repeat;
      transform: translateX(-30px);
    }
  }
}
</style>